#include "threadPool.h"


void *threadFunc(void *arg){
    threadContext_t *pthreadContext =(threadContext_t *) arg;
    int netfd;
    int sendnum=0;
    int recvnum=0;
    while(1){


        pthread_mutex_lock(&pthreadContext->pthreadPool->mutex);
        while(pthreadContext->pthreadPool->taskQueue.queueSize==0 && pthreadContext->pthreadPool->exitFlag ==0){
            pthread_cond_wait(&pthreadContext->pthreadPool->cond, &pthreadContext->pthreadPool->mutex);
        }

        if(pthreadContext->pthreadPool->exitFlag==1){
            printf("worker is gong to exit\n");
            pthread_mutex_unlock(&pthreadContext->pthreadPool->mutex);
            pthread_exit(NULL);

        }

        netfd = pthreadContext->pthreadPool->taskQueue.head->netfd;
        printf("成功出队,netfd = %d\n",netfd);
        deQueue(&pthreadContext->pthreadPool->taskQueue);
        pthread_mutex_unlock(&pthreadContext->pthreadPool->mutex);

        //初始化线程mysql
        pthread_mutex_lock(&pthreadContext->pthreadPool->sqlmutex);
        netCloud_mysql_init(&pthreadContext->pthreadMysql);
        if(netCloud_mysql_connect(pthreadContext->pthreadMysql,"localhost","root","123","netCloud",0)!=0){
            printf("thread sql connect error!\n");
            netCloud_mysql_close(pthreadContext->pthreadMysql);
            pthread_mutex_unlock(&pthreadContext->pthreadPool->sqlmutex);
            return NULL;
        } 
        pthread_mutex_unlock(&pthreadContext->pthreadPool->sqlmutex);
        TLV_t tlv;
        int pthread_user_id;
        //createFileTable(pthreadContext->pthreadMysql);
        //stackInit(pthreadContext->pstack);

        while(1){
            memset(&tlv,0,sizeof(TLV_t));
        recvTlv(netfd,&tlv);
        if(tlv.type==CMD_REGISTER){
            pthread_user_id = tackle_register(netfd,pthreadContext->pthreadMysql,&tlv);
            if(pthread_user_id < 0){
                printf("客户端注册失败\n");
                continue;
            }
            pthreadContext->pthreadFileMeta->owner_id = pthread_user_id;
            //初始化path为/,初始化filename为/
            strcpy(pthreadContext->pthreadFileMeta->path,"/");
            strcpy(pthreadContext->pthreadFileMeta->filename,"/");
            pthreadContext->pthreadFileMeta->file_type = 1;
            pthreadContext->pthreadFileMeta->parent_directory_id = -1;
            pthreadContext->pstack->mysql=pthreadContext->pthreadMysql;
            pthreadContext->pstack->owner_id = pthread_user_id;
            insert_into(pthreadContext->pthreadMysql,pthreadContext->pthreadFileMeta);
            stackInit(pthreadContext->pstack);
            break;
        }else if(tlv.type==CMD_SIGNIN){
            pthread_user_id = tackle_signin(netfd,pthreadContext->pthreadMysql,&tlv);
            if(pthread_user_id < 0){
                printf("客户端登录失败\n");
                continue;
            }
            pthreadContext->pthreadFileMeta->owner_id = pthread_user_id;
            pthreadContext->pstack->mysql=pthreadContext->pthreadMysql;
            pthreadContext->pstack->owner_id = pthread_user_id;
            stackInit(pthreadContext->pstack);
            break;
        }

        }
        
        while (1) {
            // 清空TLV结构
            memset(&tlv, 0, sizeof(TLV_t));

            // TLV_t * tlv_recv =(TLV_t *)malloc(sizeof(TLV_t));
            // memset(tlv_recv,0,sizeof(TLV_t));
            // recvTlv(netfd,tlv_recv);
            // if(tlv_recv->type == CMD_REGISTER){
            //     pthread_user_id=tackle_register(netfd,pthreadContext->pthreadMysql,tlv_recv);
            // }else if(tlv_recv->type==CMD_SIGNIN){
            //     pthread_user_id=tackle_signin(netfd,pthreadContext->pthreadMysql,tlv_recv);
            // }
            // pthreadContext->pthreadFileMeta->owner_id=pthread_user_id;
            // pthreadContext->pstack->owner_id=pthread_user_id;
            // pthreadContext->pstack->mysql=pthreadContext->pthreadMysql;
            // stackInit(pthreadContext->pstack);


            // 接收命令
            if (recvTlv(netfd, &tlv) < 0) {
                printf("recvTlv error\n");
                break;  // 连接断开或出错
            }
            printf("进入CMD处理________________________________________\n");
            printf("命令接收TLV[type=0x%02X, len=%u, seq=%u, value=%s]\n", 
                   tlv.type, tlv.length, tlv.seq, 
                   tlv.value);


            // 处理命令
            if (chooseOptin(&tlv, netfd, pthreadContext->pstack,pthreadContext->pthreadMysql,pthreadContext->pthreadFileMeta) == -1) {
                // memset(&tlv, 0, sizeof(TLV_t)); // 清空TLV结构
                // tlv.type = CMD_ERROR; // 设置错误类型
                // tlv.length = 0; // 错误响应不需要数据
                // sendTlv(netfd, &tlv); // 发送错误响应
                LOG(LOG_LEVEL_ERROR, "system", "Command processing failed");
            }
            printf("结束CMD处理________________________________________\n");
            //free(tlv_recv);
        }

        quit_user(pthread_user_id,pthreadContext->pthreadMysql);

    }

    //close(netfd);
}

//传递值变成线程上下文
int makeWorker(threadPool_t *pthreadPool){
    for(int i = 0; i < pthreadPool->tidArr.workerNum; ++i){
        pthread_create(&pthreadPool->tidArr.arr[i],NULL,threadFunc,&pthreadPool->pthreadContext[i]);
    }
    return 0;
}

